﻿Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Text

Module MainModule

    Public iniFilePath As String = Application.StartupPath & "\WatchList.ini"


    Public Const LIST_SECTION_NAME As String = "LIST_SECTION"
    Public Const LIST_KEY_NAME As String = "LIST_KEY"

    'Public Const FILE_SECTION_NAME As String = "LIST_SECTION" '根据各个文件自己的名字
    Public Const FILE_MONITOR_CREATE As String = "FILE_MONITOR_CREATE"
    Public Const FILE_MONITOR_RENAME As String = "FILE_MONITOR_RENAME"
    Public Const FILE_MONITOR_DELETE As String = "FILE_MONITOR_DELETE"
    Public Const FILE_MONITOR_CHANGE As String = "FILE_MONITOR_CHANGE"
    Public Const FILE_CREATE_MESSAGE As String = "FILE_CREATE_MESSAGE"
    Public Const FILE_RENAME_MESSAGE As String = "FILE_RENAME_MESSAGE"
    Public Const FILE_DELETE_MESSAGE As String = "FILE_DELETE_MESSAGE"
    Public Const FILE_CHANGE_MESSAGE As String = "FILE_CHANGE_MESSAGE"

    <DllImport("kernel32", CharSet:=CharSet.Unicode)>
    Private Function WritePrivateProfileString(ByVal lpApplicationName As String,
                                                  ByVal lpKeyName As String,
                                                  ByVal lpString As String,
                                                  ByVal lpFileName As String) As Integer
    End Function

    <DllImport("kernel32", CharSet:=CharSet.Unicode)>
    Private Function GetPrivateProfileString(ByVal lpApplicationName As String,
                                                   ByVal lpKeyName As String,
                                                   ByVal lpDefault As String,
                                                   ByVal lpReturnedString As StringBuilder,
                                                   ByVal nSize As Integer,
                                                   ByVal lpFileName As String) As Integer
    End Function

    Public Function Read(ByVal section As String, ByVal key As String) As String
        Dim returnValue As New StringBuilder(255)
        GetPrivateProfileString(section, key, "", returnValue, 255, iniFilePath)
        Return returnValue.ToString()
    End Function

    Public Sub Write(ByVal section As String, ByVal key As String, ByVal value As String)
        WritePrivateProfileString(section, key, value, iniFilePath)
    End Sub

    Public Function Delete(ByVal section As String) As Boolean
        If Not File.Exists(iniFilePath) Then
            Console.WriteLine("INI file does not exist.")
            Return True
        End If

        'section 
        If Not "".Equals(section) Then
            Dim lines As String() = File.ReadAllLines(iniFilePath, Encoding.GetEncoding("SHIFT-JIS"))
            Dim updatedLines As New List(Of String)()
            Dim isInSection As Boolean = False

            For Each line As String In lines
                ' 检查当前行是否开始新的section  
                If line.StartsWith("[") AndAlso line.EndsWith("]") Then
                    Dim currentSection As String = line.TrimStart("[").TrimEnd("]")
                    ' 如果当前section是要删除的section，则设置标志并跳过该行  
                    If currentSection.Equals(section, StringComparison.OrdinalIgnoreCase) Then
                        isInSection = True
                        Continue For
                    Else
                        ' 如果之前在要删除的section中，现在不在了，开始添加行到更新后的列表  
                        isInSection = False
                    End If
                End If

                ' 如果不在要删除的section中，则添加行到更新后的列表  
                If Not isInSection Then
                    updatedLines.Add(line)
                End If
            Next

            ' 将更新后的内容写回INI文件  
            File.WriteAllLines(iniFilePath, updatedLines.ToArray(), Encoding.GetEncoding("SHIFT-JIS"))
            Console.WriteLine("Section deleted successfully.")
        End If

        Return True
    End Function


    Public Function Save(ByVal dog As WatchDog) As Boolean

        'Gate Change
        MainModule.Write(dog.dogName, "WATCH_GATE", dog.watchGate)
        'Clear bones
        MainModule.Write(dog.dogName, "BONES", "")

        For Each item As Bone In dog.boneList
            'add bone
            Dim strBones As String
            strBones = MainModule.Read(dog.dogName, "BONES")
            If "".Equals(strBones) Then
                MainModule.Write(dog.dogName, "BONES", item.FileName)
            Else
                MainModule.Write(dog.dogName, "BONES", strBones & "," & item.FileName)
            End If

            'delete old bone
            MainModule.Delete(dog.dogName & "@\@" & item.FileName)

            MainModule.Write(dog.dogName & "@\@" & item.FileName, FILE_MONITOR_CREATE, item.NeedMonitorCreate)
            MainModule.Write(dog.dogName & "@\@" & item.FileName, FILE_CREATE_MESSAGE, item.CreateMessage)
            MainModule.Write(dog.dogName & "@\@" & item.FileName, FILE_MONITOR_RENAME, item.NeedMonitorRename)
            MainModule.Write(dog.dogName & "@\@" & item.FileName, FILE_RENAME_MESSAGE, item.RenameMessage)
            MainModule.Write(dog.dogName & "@\@" & item.FileName, FILE_MONITOR_DELETE, item.NeedMonitorDelete)
            MainModule.Write(dog.dogName & "@\@" & item.FileName, FILE_DELETE_MESSAGE, item.DeleteMessage)
            MainModule.Write(dog.dogName & "@\@" & item.FileName, FILE_MONITOR_CHANGE, item.NeedMonitorChange)
            MainModule.Write(dog.dogName & "@\@" & item.FileName, FILE_CHANGE_MESSAGE, item.ChangeMessage)

        Next

        Dim strKennelKey As String
        strKennelKey = MainModule.Read("KENNEL_SECTION", "KENNEL_KEY")

        If Not strKennelKey.IndexOf(dog.dogName) >= 0 Then

            If "".Equals(strKennelKey) Then
                strKennelKey = dog.dogName
            Else
                strKennelKey = strKennelKey & "," & dog.dogName
            End If
            MainModule.Write("KENNEL_SECTION", "KENNEL_KEY", strKennelKey)
        End If


        Return True
    End Function

End Module
